package com.ywu.data.source.standard.service;

import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.gson.GsonUtils;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.data.source.standard.constant.YwuDataSourceConstant;
import com.ywu.data.source.standard.data.YwuDataExecRepository;
import com.ywu.data.source.standard.data.YwuDataSourceRepository;
import com.ywu.data.source.standard.data.YwuDataTableRepository;
import com.ywu.data.source.standard.dto.*;
import com.ywu.data.source.standard.entity.YwuDataExecEntity;
import com.ywu.data.source.standard.entity.YwuDataExecVersionEntity;
import com.ywu.data.source.standard.entity.YwuDataSourceEntity;
import com.ywu.data.source.standard.entity.YwuDataTableEntity;
import com.ywu.data.source.standard.source.YwuExecSqlCoreService;
import com.ywu.data.source.standard.source.sql.YwuBuildSqlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName YwuDataExecService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/31 17:46
 * @Version 1.0
 **/
@Service
@Slf4j
public class YwuDataExecService {
    @Autowired
    private YwuDataExecRepository repository;

    @Autowired
    private YwuBuildSqlUtil buildSqlUtil;

    @Autowired
    private YwuDataExecVersionService ywuDataExecVersionService;

    @Autowired
    private YwuExecSqlCoreService ywuExecSqlCoreService;

    @Autowired
    private YwuDataSourceRepository ywuDataSourceRepository;

    @Autowired
    private YwuDataTableRepository ywuDataTableRepository;

    /**
    * @Author GroundDemo
    * @Description 创建执行服务
    * @Date 18:01 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> createDataExec(YwuDataExecDto ywuDataExecDto) {
        // 判断当前编码是否存在
        YwuDataExecEntity search = new YwuDataExecEntity();
        search.setCode(ywuDataExecDto.getCode());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuDataExecEntity queryRes = repository.getEntity(search);
        if (Objects.nonNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.CODE_HAS_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.CODE_HAS_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.CODE_HAS_EXISTS_ERROR_MESSAGE);
        }
        // 复杂查询单独处理，其他操作不影响
        if (Objects.nonNull(ywuDataExecDto.getExecType()) && ywuDataExecDto.getExecType().equals(YwuDataExecEntity.COMPLEX_QUERY_EXEC_TYPE)) {
            return createComplexQueryDataExec(ywuDataExecDto);
        }
        // 构建参数
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(ywuDataExecDto, ywuDataExecEntity);
        ywuDataExecEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        ywuDataExecEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecEntity.setId(TimerIdGenerateUtil.nextId());
        ywuDataExecEntity.setStatus(YwuDataExecEntity.DRAFT_STATUS);
        ywuDataExecEntity.setCurrentVersion(0);
        ywuDataExecEntity.setHasNotReleased(false);
        ywuDataExecEntity.setExecConditionStr(GsonUtils.serialize(ywuDataExecDto.getExecCondition()));
        ywuDataExecEntity.setExecFieldsStr(GsonUtils.serialize(ywuDataExecDto.getExecFields()));
        // 生成sql
        String execSql = buildSqlUtil.buildSql(ywuDataExecEntity);
        ywuDataExecEntity.setExecSql(execSql);
        // 插入数据
        repository.insertDataExec(ywuDataExecEntity);
        return CommonResult.success(ywuDataExecEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 创建复杂查询执行服务
    * @Date 18:02 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    private CommonResult<Object> createComplexQueryDataExec(YwuDataExecDto ywuDataExecDto) {
        // 构建参数
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(ywuDataExecDto, ywuDataExecEntity);
        ywuDataExecEntity.setStatus(YwuDataExecEntity.DRAFT_STATUS);
        ywuDataExecEntity.setCurrentVersion(0);
        ywuDataExecEntity.setHasNotReleased(false);
        ywuDataExecEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        ywuDataExecEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecEntity.setId(TimerIdGenerateUtil.nextId());
        // 数据表编码列表采用逗号分割
        ywuDataExecEntity.setDataTableCode(ywuDataExecDto.getDataTableCodeList().stream().collect(Collectors.joining(",")));
        ywuDataExecEntity.setComplexQueryFieldListStr(GsonUtils.serialize(ywuDataExecDto.getComplexQueryFieldList()));
        if (CollectionUtils.isEmpty(ywuDataExecDto.getExecCondition())) {
            ywuDataExecEntity.setConditionFields("[]");
        } else {
            ywuDataExecEntity.setExecConditionStr(GsonUtils.serialize(ywuDataExecDto.getConditionFields()));
        }
        ywuDataExecEntity.setComplexQueryTableJoinListStr(GsonUtils.serialize(ywuDataExecDto.getComplexQueryTableJoinList()));
        ywuDataExecEntity.setGroupFieldsStr(GsonUtils.serialize(ywuDataExecDto.getGroupFields()));
        ywuDataExecEntity.setGroupConditionStr(GsonUtils.serialize(ywuDataExecDto.getGroupCondition()));
        ywuDataExecEntity.setSortFieldsStr(GsonUtils.serialize(ywuDataExecDto.getSortFields()));
        ywuDataExecEntity.setEnablePageInfo(ywuDataExecDto.getEnablePageInfo());
        ywuDataExecEntity.setExecFieldsStr("[]");
        // 生成sql
        String execSql = buildSqlUtil.buildSql(ywuDataExecEntity);
        ywuDataExecEntity.setExecSql(execSql);
        // 插入数据
        repository.insertDataExec(ywuDataExecEntity);
        return CommonResult.success(ywuDataExecEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 修改执行服务
    * @Date 18:02 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateDataExec(YwuDataExecDto ywuDataExecDto) {
        // 查询当前数据是否存在
        YwuDataExecEntity search = new YwuDataExecEntity();
        search.setId(ywuDataExecDto.getId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuDataExecEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
        }
        // 复杂查询单独处理，其他操作不影响
        if (Objects.nonNull(ywuDataExecDto.getExecType()) && ywuDataExecDto.getExecType().equals(YwuDataExecEntity.COMPLEX_QUERY_EXEC_TYPE)) {
            return updateComplexQueryDataExec(ywuDataExecDto, queryRes);
        }
        // 构建参数
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(ywuDataExecDto, ywuDataExecEntity);
        ywuDataExecEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        ywuDataExecEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        ywuDataExecEntity.setExecFieldsStr(GsonUtils.serialize(ywuDataExecEntity.getExecFields()));
        ywuDataExecEntity.setExecConditionStr(GsonUtils.serialize(ywuDataExecEntity.getExecCondition()));
        // 构建sql
        String execSql = buildSqlUtil.buildSql(ywuDataExecEntity);
        ywuDataExecEntity.setExecSql(execSql);
        // 根据当前元数据的状态执行不同的方法
        if (queryRes.getStatus().equals(YwuDataExecEntity.DRAFT_STATUS)) {
            updateDraftInfo(ywuDataExecEntity);
        } else {
            ywuDataExecVersionService.updateVersionInfo(ywuDataExecEntity);
            // 修改是否存在草稿版本的标志位
            YwuDataExecEntity update = new YwuDataExecEntity();
            update.setHasNotReleased(true);
            update.setId(ywuDataExecEntity.getId());
            update.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
            repository.updateDataExec(update);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新复杂查询执行服务
    * @Date 18:03 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    private CommonResult<Object> updateComplexQueryDataExec(YwuDataExecDto ywuDataExecDto, YwuDataExecEntity queryRes) {
        // 构建参数
        YwuDataExecEntity ywuDataExecEntity = new YwuDataExecEntity();
        BeanUtils.copyProperties(ywuDataExecDto, ywuDataExecEntity);
        ywuDataExecEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        // 数据表编码列表采用逗号分割
        ywuDataExecEntity.setDataTableCode(ywuDataExecDto.getDataTableCodeList().stream().collect(Collectors.joining(",")));
        ywuDataExecEntity.setEnablePageInfo(ywuDataExecDto.getEnablePageInfo());
        ywuDataExecEntity.setSortFieldsStr(GsonUtils.serialize(ywuDataExecDto.getSortFields()));
        ywuDataExecEntity.setComplexQueryTableJoinListStr(GsonUtils.serialize(ywuDataExecDto.getComplexQueryTableJoinList()));
        ywuDataExecEntity.setGroupFieldsStr(GsonUtils.serialize(ywuDataExecDto.getGroupFields()));
        ywuDataExecEntity.setGroupConditionStr(GsonUtils.serialize(ywuDataExecDto.getGroupCondition()));
        ywuDataExecEntity.setComplexQueryFieldListStr(GsonUtils.serialize(ywuDataExecDto.getComplexQueryFieldList()));
        if (CollectionUtils.isEmpty(ywuDataExecDto.getExecCondition())) {
            ywuDataExecEntity.setExecConditionStr("[]");
        } else {
            ywuDataExecEntity.setExecConditionStr(GsonUtils.serialize(ywuDataExecDto.getExecCondition()));
        }
        ywuDataExecEntity.setExecFieldsStr("[]");
        ywuDataExecEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        // 构建sql
        String execSql = buildSqlUtil.buildSql(ywuDataExecEntity);
        ywuDataExecEntity.setExecSql(execSql);
        // 根据当前元数据的状态执行不同的方法
        if (queryRes.getStatus().equals(YwuDataExecEntity.DRAFT_STATUS)) {
            updateDraftInfo(ywuDataExecEntity);
        } else {
            ywuDataExecVersionService.updateVersionInfo(ywuDataExecEntity);
            // 修改是否存在草稿版本的标志位
            YwuDataExecEntity update = new YwuDataExecEntity();
            update.setId(ywuDataExecEntity.getId());
            update.setHasNotReleased(true);
            update.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
            repository.updateDataExec(update);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新草稿状态的数据
    * @Date 21:13 2024/10/31
    * @Param [ywuDataExecEntity]
    * @return void
    **/
    private void updateDraftInfo(YwuDataExecEntity ywuDataExecEntity) {
        ywuDataExecEntity.setStatus(YwuDataExecEntity.DRAFT_STATUS);
        repository.updateDataExec(ywuDataExecEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 删除执行服务
    * @Date 18:03 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> deleteDataExec(YwuDataExecDto ywuDataExecDto) {
        // 判断数据是否存在
        YwuDataExecEntity search = new YwuDataExecEntity();
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        search.setId(ywuDataExecDto.getId());
        YwuDataExecEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
        }
        // 删除数据时先删除所有的历史版本数据
        ywuDataExecVersionService.deleteAllVersionByResourceId(ywuDataExecDto.getId());
        // 删除数据
        repository.deleteDataExec(ywuDataExecDto);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 分页查询执行服务
    * @Date 18:04 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryDataExecPageList(YwuDataExecDto ywuDataExecDto) {
        // 设置租户ID
        ywuDataExecDto.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        // 查询数量
        int count = repository.queryCount(ywuDataExecDto);

        // 构建分页参数
        PageService.execPageStartAndEnd(ywuDataExecDto.getPageInfo());
        // 查询列表
        List<YwuDataExecEntity> queryList = repository.queryList(ywuDataExecDto);
        if (CollectionUtils.isEmpty(queryList)) {
            PageList<YwuDataExecEntity> res = new PageList<>(ywuDataExecDto.getPageInfo().getPageIndex(), ywuDataExecDto.getPageInfo().getPageSize(),
                    0, new ArrayList<>());
            return CommonResult.success(res);
        }
        // 查询所有ID所对应的草稿版本的数据，后续进行填充
        List<String> resourceIds = queryList.stream().map(YwuDataExecEntity::getId).collect(Collectors.toList());
        List<YwuDataExecVersionEntity> versionEntityList = ywuDataExecVersionService.queryDraftListByResourceIds(resourceIds);
        if (CollectionUtils.isEmpty(versionEntityList)) {
            queryList.forEach(res -> {
                if (res.getExecType().equals(YwuDataExecEntity.COMPLEX_QUERY_EXEC_TYPE)) {
                    res.setExecConditionView(res.getExecConditionStr());
                    res.setExecFieldsView(res.getComplexQueryFieldListStr());
                    String[] split = res.getDataTableCode().split(",");
                    res.setDataTableCodeList(Arrays.asList(split));
                } else {
                    res.setExecFieldsView(res.getExecFieldsStr());
                    res.setExecConditionView(res.getExecConditionStr());
                }
            });
            PageList<YwuDataExecEntity> resPageList = new PageList<>(ywuDataExecDto.getPageInfo().getPageIndex(), ywuDataExecDto.getPageInfo().getPageSize(),
                    count, queryList);
            return CommonResult.success(resPageList);
        }
        // 构建MAP，方便后续填充
        Map<String, YwuDataExecVersionEntity> ywuThirdExecVersionEntityMap = versionEntityList.stream().collect(Collectors.toMap(YwuDataExecVersionEntity::getResourceId, res -> res,
                (v1, v2) -> v1));
        // 分页查询不需要文档数据，将其置空
        queryList.forEach(res -> {
            if (ywuThirdExecVersionEntityMap.containsKey(res.getId())) {
                YwuDataExecVersionEntity entity = ywuThirdExecVersionEntityMap.get(res.getId());
                res.setExecConditionStr(entity.getExecConditionStr());
                res.setExecFieldsStr(entity.getExecFieldsStr());
                res.setComplexQueryFieldListStr(entity.getComplexQueryFieldListStr());
                res.setComplexQueryTableJoinListStr(entity.getComplexQueryTableJoinListStr());
                res.setGroupConditionStr(entity.getGroupConditionStr());
                res.setGroupFieldsStr(entity.getGroupFieldsStr());
                res.setSortFieldsStr(entity.getSortFieldsStr());
                res.setEnablePageInfo(entity.getEnablePageInfo());
                res.setGroupConditionFields(entity.getGroupConditionFields());
            }
            if (res.getExecType().equals(YwuDataExecEntity.COMPLEX_QUERY_EXEC_TYPE)) {
                res.setExecFieldsView(res.getComplexQueryFieldListStr());
                res.setExecConditionView(res.getExecConditionStr());
                String[] split = res.getDataTableCode().split(",");
                res.setDataTableCodeList(Arrays.asList(split));
            } else {
                res.setExecFieldsView(res.getExecFieldsStr());
                res.setExecConditionView(res.getExecConditionStr());
            }
        });

        PageList<YwuDataExecEntity> res = new PageList<>(ywuDataExecDto.getPageInfo().getPageIndex(), ywuDataExecDto.getPageInfo().getPageSize(),
                count, queryList);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 发布数据
    * @Date 21:49 2024/10/31
    * @Param [ywuDataExecDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> publishDataExec(YwuDataExecDto ywuDataExecDto) {
        // 判断数据是否存在
        YwuDataExecEntity search = new YwuDataExecEntity();
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        search.setId(ywuDataExecDto.getId());
        YwuDataExecEntity queryRes = repository.getEntity(search);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                    .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE)
                    .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE);
        }
        // 根据当前数据的状态进行不同的操作
        if (queryRes.getStatus().equals(YwuDataExecEntity.DRAFT_STATUS)) {
            // 发布草稿状态数据
            publishDraftInfo(queryRes);
        } else {
            // 发布草稿状态数据
            ywuDataExecVersionService.publishVersionInfo(queryRes);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 发布草稿状态的数据
    * @Date 21:51 2024/10/31
    * @Param [queryRes]
    * @return void
    **/
    private void publishDraftInfo(YwuDataExecEntity queryRes) {
        // 在version表中添加一条版本记录
        ywuDataExecVersionService.addVersionInfo(queryRes);
        // 当前数据的状态为修改为已发布无草稿
        queryRes.setStatus(YwuDataExecEntity.ONLINE_STATUS);
        queryRes.setHasNotReleased(false);
        queryRes.setCurrentVersion(queryRes.getCurrentVersion() + 1);
        queryRes.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.updateDataExec(queryRes);
    }

    /**
    * @Author GroundDemo
    * @Description 执行脚本
    * @Date 13:44 2024/11/20
    * @Param [ywuDataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> dataExec(YwuDataExecVersionDto ywuDataExecVersionDto) {
        YwuDataExecEntity execEntity = new YwuDataExecEntity();
        // 入参中存在版本，则查询历史版本执行数据
        if (Objects.isNull(ywuDataExecVersionDto.getVersion())) {
            YwuDataExecEntity search = new YwuDataExecEntity();
            search.setId(ywuDataExecVersionDto.getResourceId());
            search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
            execEntity = repository.getEntity(search);
            if (Objects.isNull(execEntity)) {
                return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                        .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                        .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
            }
        } else {
            ywuDataExecVersionDto.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
            YwuDataExecVersionEntity entity = ywuDataExecVersionService.getEntity(ywuDataExecVersionDto);
            if (Objects.isNull(entity)) {
                return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                        .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                        .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
            }
            BeanUtils.copyProperties(entity, execEntity);
        }
        execEntity.setExecFields(GsonUtils.deserializeList(execEntity.getExecFieldsStr(), String.class));
        execEntity.setExecCondition(GsonUtils.deserializeList(execEntity.getExecConditionStr(), YwuDataConditionDto.class));
        // 根据dataSourceCode关联查询dataSourceType
        YwuDataSourceEntity ywuDataSourceEntity = new YwuDataSourceEntity();
        ywuDataSourceEntity.setCode(execEntity.getDataSourceCode());
        YwuDataSourceEntity dataSourceEntity = ywuDataSourceRepository.getEntity(ywuDataSourceEntity);
        execEntity.setDataSourceType(dataSourceEntity.getDataSourceType());
        execEntity.setDataSourceTenantId(dataSourceEntity.getTenantId());
        execEntity.setFieldParam(ywuDataExecVersionDto.getFieldParam());
        execEntity.setConditionParam(ywuDataExecVersionDto.getConditionParam());
        execEntity.setGroupConditionParam(ywuDataExecVersionDto.getGroupConditionParam());
        execEntity.setPageFields(ywuDataExecVersionDto.getPageFields());
        try {
            switch (execEntity.getExecType()) {
                case YwuDataExecEntity.DELETE_EXEC_TYPE:
                    ywuExecSqlCoreService.deleteInfo(execEntity);
                    return CommonResult.success("SUCCESS");
                case YwuDataExecEntity.INSERT_EXEC_TYPE:
                    ywuExecSqlCoreService.insertIntoInfo(execEntity);
                    return CommonResult.success("SUCCESS");
                case YwuDataExecEntity.UPDATE_EXEC_TYPE:
                    ywuExecSqlCoreService.updateInfo(execEntity);
                    return CommonResult.success("SUCCESS");
                case YwuDataExecEntity.QUERY_EXEC_TYPE:
                    List<Map<String, Object>> queryRes = ywuExecSqlCoreService.queryInfo(execEntity);
                    return CommonResult.success(queryRes);
                case YwuDataExecEntity.COMPLEX_QUERY_EXEC_TYPE:
                    List<Map<String, Object>> complexQueryRes = ywuExecSqlCoreService.complexQueryInfo(execEntity);
                    return CommonResult.success(complexQueryRes);
                default:
                    return CommonResult.success("null");
            }
        } catch (Exception e) {
            log.error("data.exec.error {}", e.getMessage());
            return CommonResult.error().setCode(YwuDataSourceConstant.DATA_EXEC_ERROR_CODE)
                    .setErrorCode(YwuDataSourceConstant.DATA_EXEC_ERROR_ERROR_CODE)
                    .setMessage(YwuDataSourceConstant.DATA_EXEC_ERROR_ERROR_MESSAGE);
        }
    }

    /**
    * @Author GroundDemo
    * @Description 调试字段查询
    * @Date 16:11 2024/11/20
    * @Param [ywuDataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryExecDebugParam(YwuDataExecVersionDto ywuDataExecVersionDto) {
        YwuDataExecEntity execEntity = new YwuDataExecEntity();
        // 根据resourceId和version查询数据
        if (Objects.isNull(ywuDataExecVersionDto.getVersion())) {
            YwuDataExecEntity search = new YwuDataExecEntity();
            search.setId(ywuDataExecVersionDto.getResourceId());
            execEntity = repository.getEntity(search);
            if (Objects.isNull(execEntity)) {
                return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                        .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                        .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
            }
        } else {
            YwuDataExecVersionEntity entity = ywuDataExecVersionService.getEntity(ywuDataExecVersionDto);
            if (Objects.isNull(entity)) {
                return CommonResult.error().setCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_CODE)
                        .setErrorCode(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_CODE)
                        .setMessage(YwuDataSourceConstant.RESOURCE_IS_NOT_EXISTS_ERROR_MESSAGE);
            }
            BeanUtils.copyProperties(entity, execEntity);
        }
        // 构建响应参数
        YwuDataExecVersionDto res = new YwuDataExecVersionDto();
        List<String> execFields = GsonUtils.deserializeList(execEntity.getExecFieldsStr(), String.class);
        ArrayList<YwuExecCommonParamDto> fieldParam = new ArrayList<>();
        for (String field : execFields) {
            YwuExecCommonParamDto ywuExecCommonParamDto = new YwuExecCommonParamDto();
            ywuExecCommonParamDto.setFieldName(field);
            fieldParam.add(ywuExecCommonParamDto);
        }
        List<YwuDataConditionDto> condition = GsonUtils.deserializeList(execEntity.getExecConditionStr(), YwuDataConditionDto.class);
        ArrayList<YwuExecCommonParamDto> conditionParam = new ArrayList<>();
        for (YwuDataConditionDto c : condition) {
            YwuExecCommonParamDto ywuExecCommonParamDto = new YwuExecCommonParamDto();
            ywuExecCommonParamDto.setFieldName(c.getFieldName());
            conditionParam.add(ywuExecCommonParamDto);
        }
        List<YwuDataExecComplexQueryGroupConditionDto> groupCondition = GsonUtils.deserializeList(execEntity.getGroupConditionStr(), YwuDataExecComplexQueryGroupConditionDto.class);
        if (!CollectionUtils.isEmpty(groupCondition)) {
            ArrayList<YwuExecCommonParamDto> groupConditionParam = new ArrayList<>();
            for (YwuDataExecComplexQueryGroupConditionDto c : groupCondition) {
                YwuExecCommonParamDto ywuExecCommonParamDto = new YwuExecCommonParamDto();
                ywuExecCommonParamDto.setFieldName(c.getFieldName());
                groupConditionParam.add(ywuExecCommonParamDto);
            }
            res.setGroupConditionParam(groupConditionParam);
        }
        res.setFieldParam(fieldParam);
        res.setConditionParam(conditionParam);
        res.setExecType(execEntity.getExecType());
        res.setEnablePageInfo(execEntity.getEnablePageInfo());
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 调试字段查询
    * @Date 16:11 2024/11/20
    * @Param [ywuDataExecVersionDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryExecDebugParamNew(YwuDataExecParamQueryDto ywuDataExecParamQueryDto) {
        if (CollectionUtils.isEmpty(ywuDataExecParamQueryDto.getDataTableCodeList())) {
            if (StringUtils.isEmpty(ywuDataExecParamQueryDto.getDataTableCode())) {
                return CommonResult.success(new ArrayList<>());
            }
            YwuDataTableEntity ywuDataTableEntity = new YwuDataTableEntity();
            ywuDataTableEntity.setCode(ywuDataExecParamQueryDto.getDataTableCode());
            ywuDataTableEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
            YwuDataTableEntity queryRes = ywuDataTableRepository.getEntity(ywuDataTableEntity);
            List<YwuDataTableFieldDto> ywuDataTableFieldList = GsonUtils.deserializeList(queryRes.getFieldInfoStr(), YwuDataTableFieldDto.class);
            return CommonResult.success(ywuDataTableFieldList);
        } else {
            List<YwuDataTableFieldDto> resList = new ArrayList<>();
            for (String code : ywuDataExecParamQueryDto.getDataTableCodeList()) {
                YwuDataTableEntity ywuDataTableEntity = new YwuDataTableEntity();
                ywuDataTableEntity.setCode(code);
                ywuDataTableEntity.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
                YwuDataTableEntity queryRes = ywuDataTableRepository.getEntity(ywuDataTableEntity);
                List<YwuDataTableFieldDto> ywuDataTableFieldList = GsonUtils.deserializeList(queryRes.getFieldInfoStr(), YwuDataTableFieldDto.class);
                for (YwuDataTableFieldDto field : ywuDataTableFieldList) {
                    field.setName(queryRes.getTableName() + "." + field.getName());
                }
                resList.addAll(ywuDataTableFieldList);
            }
            return CommonResult.success(resList);
        }
    }
}
